package com.ruoyi.inspection.controller;

import com.google.common.base.Preconditions;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.inspection.domain.GovernCheckTask;
import com.ruoyi.inspection.domain.GovernCheckTaskInfo;
import com.ruoyi.inspection.service.GovernCheckTaskInfoService;
import com.ruoyi.inspection.service.GovernTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 政府专项检查表(GovernCheckTaskInfo)表控制层
 *
 * @author makejava
 * @since 2024-06-19 16:15:55
 */
@Api("下发任务接口")
@RestController
@RequestMapping("/check/governCheckTaskInfo")
public class GovernCheckTaskInfoController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private GovernCheckTaskInfoService governCheckTaskInfoService;
    @Resource
    private GovernTaskService governTaskService;
    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询
     *
     * @param governCheckTaskInfo 筛选条件
     * @return 查询结果
     */
    @ApiOperation("下发任务列表")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskInfo:list')")
    @GetMapping("/list")
    public TableDataInfo queryByPage(GovernCheckTaskInfo governCheckTaskInfo) {
        startPage();
        List<GovernCheckTaskInfo> governCheckTaskInfos = this.governCheckTaskInfoService.queryByPage(governCheckTaskInfo);
        return getDataTable(governCheckTaskInfos);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param governTaskInfoId 主键
     * @return 单条数据
     */
    @ApiOperation("查看任务")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskInfo:query')")
    @GetMapping("/{governTaskInfoId}")
    public AjaxResult queryById(@PathVariable("governTaskInfoId") Long governTaskInfoId) {
        return AjaxResult.success(this.governCheckTaskInfoService.queryById(governTaskInfoId));
    }

    /**
     * 新增市、省、部级专项检查任务
     *
     * @param governCheckTaskInfo 新增任务对象
     * @return
     */
    @ApiOperation("新增省市部任务")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskInfo:add')")
    @PostMapping("/saveTaskInfo")
    public AjaxResult insertTaskInfo(@RequestBody GovernCheckTaskInfo governCheckTaskInfo){
        String governCheckTaskId = governCheckTaskInfo.getGovernCheckTaskId();
        String governCheckTaskTypeName = governCheckTaskInfo.getGovernCheckTaskTypeName();

        return AjaxResult.success(governCheckTaskInfoService.saveTaskInfo(governCheckTaskId, governCheckTaskTypeName, getUsername()));
    }


    /**
     * 编辑数据
     *
     * @param governCheckTaskInfo 实体
     * @return 编辑结果
     */
    @ApiOperation("修改任务完成结果")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskInfo:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody GovernCheckTaskInfo governCheckTaskInfo) {
        return AjaxResult.success(this.governCheckTaskInfoService.update(governCheckTaskInfo, getUsername()));
    }

    /**
     * 删除数据
     *
     * @param governTaskInfoId 专项检查ID
     * @return 删除是否成功
     */
    @ApiOperation("删除专项检查")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskInfo:remove')")
    @DeleteMapping
    public AjaxResult deleteById(Long governTaskInfoId) {
        return AjaxResult.success(this.governCheckTaskInfoService.deleteById(governTaskInfoId));
    }

    /**
     * 批量删除数据
     */
//    @PreAuthorize("@ss.hasPermi('task:taskInfo:remove')")
    @Log(title = "专项检查", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskInfo:remove')")
    @DeleteMapping("/{governTaskInfoIds}")
    public AjaxResult remove(@PathVariable String[] governTaskInfoIds)
    {
        if (StringUtils.isEmpty(governTaskInfoIds))  { throw new ServiceException("参数异常"); }

        List<Long> ids = Arrays.stream(governTaskInfoIds)
                .map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        return toAjax(governCheckTaskInfoService.deleteByIds( ids.toArray(new Long[ids.size()])) );
    }

    /**
     * 任务分解接口
     * @param governTaskInfoId 专项检查ID
     * @return
     */
    @ApiOperation("任务分解")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskInfo:add')")
    @GetMapping("/taskSalvage")
    public AjaxResult taskSalvage(HttpServletRequest request, Long governTaskInfoId) {
        LoginUser user = tokenService.getLoginUser(request);
        String userName = user.getUsername();
        governCheckTaskInfoService.taskSalvage(governTaskInfoId, userName);
        return AjaxResult.success();
    }



//    /**
//     * 新增数据
//     *
//     * @param governCheckTaskInfo 实体
//     * @return 新增结果
//     */
//    @PostMapping
//    public AjaxResult add(GovernCheckTaskInfo governCheckTaskInfo) {
//        return AjaxResult.success(this.governCheckTaskInfoService.insert(governCheckTaskInfo));
//    }
}

